/**
 * 系统角色js
 */
var sysRole= {
	curPage:1,
	pageSize:10,
	dataCache:null,
	obj:null,
	
	startEvent:function(){
		//按回车键登录事件
		document.onkeydown = function(e){ 
    		var ev = document.all ? window.event : e;
    		if(ev.keyCode==13) {
    			sysRole.curPage=1;
    			sysRole.setPageChange(null);
    			sysRole.search();
     		}
		};
	},
	
	bindEvent:function(){	
		//查询
		$('#searchData').click(function(){
			sysRole.curPage=1;
			sysRole.setPageChange(null);
			sysRole.search();
		});
		
		$('#addData').on('click',function(){
			var path=APP_ADDRESS+'mgr/sysMgr/sysRoleAdd.jsp';
			art.dialog.open(path, {
			    title: '添加系统角色',
			    width: 600,
			    height:400,
			   ok: function () {
				   var returnStatus=this.iframe.contentWindow.submitForm();
				   if(returnStatus){
					   sysRole.setPageChange(null);
					   sysRole.search();
				   }
				   return false;
			    },
			    cancel: true
			});
		});
	},
	
	//设置分页
	setPageChange:function(delCurPage){
		var url = 'mgr/SysRoleCtrl/getSysRoleList';
		var name=$.trim($('#searchModForm input[name="name"]').val());
		var params={
			'curPage':1,
			'pageSize':99999,
			'name':name
		};
		var cb = function(data) {
			if (data.resultCode == 200) {
				var totalCount = data.size;
				var pageTotal = (totalCount / sysRole.pageSize > 0) ? Math.ceil(totalCount/ sysRole.pageSize) : 1;
				pageHelper.init(pageTotal,totalCount,sysRole.search,delCurPage);  //翻页事件
			}
		};
		sendApiNoLoad(url, params, cb, 'POST');
	},
	
	//查询系统角色
	search:function(pageIdx){
		var url = 'mgr/SysRoleCtrl/getSysRoleList';
		if (typeof(pageIdx) != "undefined"){ 
			sysRole.curPage=pageIdx;
		}
		var name=$.trim($('#searchModForm input[name="name"]').val());
		var params={
			'curPage':sysRole.curPage,
			'pageSize':sysRole.pageSize,
			'name':name
		};
		var cb = function(data) {
			if (data.resultCode == 200) {
				$('#tableList tbody').empty();
				var tr='';
				if(data.result!=null&&data.result.length>0){
					for(var i=0;i<data.result.length;i++){
						
						var singleObj=data.result[i];
						
						localStorage.setItem(singleObj.sysRole.id + "_SysRole_Key", JSON.stringify(singleObj));
						
						tr += '<tr><td><input type="hidden" name="id" value="'+singleObj.sysRole.id+'"/>'
							+ (i+1)
							+ '</td><td>'
							+ singleObj.sysRole.name
							+ '</td><td>'
							+ singleObj.sysRole.desc
							+ '</td><td>'
							+ singleObj.sysRole.creator
							+ '</td><td>'
							+ getSmpFormatDateByLong(singleObj.sysRole.createdts, true)
							+ '</td><td>'
							+'<a type="button" name="sysRole_99" class="btn btn-primary btn-xs lookData"  style="margin-left: 5px;display:none;">'
							+'<i class="fa fa-reorder"></i> 查看 </a>'
							+'<a type="button" name="sysRole_96" class="btn btn-success btn-xs updateData" style="margin-left: 5px;display:none;">'
							+'<i class="fa fa-edit"></i>修改</a>'
							+'<a type="button" name="sysRole_97" class="btn btn-default btn-xs deleteData" style="margin-left: 5px;display:none;">'
							+'<i class="fa fa-remove"></i> 删除</a>'
							+'</td></tr>';
					}
				}else{
					tr='<tr><td colspan="6" style="text-align: center;color: red;">暂无数据</td></tr>';
				}
				$('#tableList tbody').html(tr);
				sysRole.bindEventOtherBtn();
				jsUtils.controlFunctionBtn();
			}
		};
		sendApiNoLoad(url, params, cb, 'POST');
	},
	
	bindEventOtherBtn:function(){
		$('.deleteData').unbind();
		$('.deleteData').on('click',function(){
			var thisObj=$(this);
			art.dialog.confirm('你确认删除？', function(){
				var id=thisObj.parent().parent().find('input[name="id"]').eq(0).val();
				var url = 'mgr/SysRoleCtrl/deleteSysRole';
				var params = {
						'id':id
				};
				var cb = function(data){
					alert('删除成功！');
					sysRole.setPageChange(sysRole.curPage);
					sysRole.search(sysRole.curPage);
				}
				sendApiNoLoad(url, params, cb, 'POST');
			}, function(){
			});
		});
		$('.updateData').unbind();
		$('.updateData').on('click',function(){
			var thisObj=$(this);
			var id=thisObj.parent().parent().find('input[name="id"]').eq(0).val();
			art.dialog.data('id', id);
			var path=APP_ADDRESS+'mgr/sysMgr/sysRoleUpdate.jsp';
			art.dialog.open(path, {
			    title: '修改系统角色',
			    width: 600,
			    height:400,
			   ok: function () {
				   var returnStatus=this.iframe.contentWindow.submitForm();
				   if(returnStatus){
					   sysRole.setPageChange(sysRole.curPage);
					   sysRole.search(sysRole.curPage);
				   }
				   return false;
			    },
			    cancel: true
			});
		});
		$('.lookData').unbind();
		$('.lookData').on('click',function(){
			var thisObj=$(this);
			var id=thisObj.parent().parent().find('input[name="id"]').eq(0).val();
			art.dialog.data('lokId', id);
			var path=APP_ADDRESS+'mgr/sysMgr/sysRoleLook.jsp';
			art.dialog.open(path, {
			    title: '查看系统角色',
			    width: 500,
			    height:300,
			    cancel: true
			});
		});
	},
		
	init : function() {
		sysRole.setPageChange(null);
		sysRole.search();
		sysRole.startEvent();
		sysRole.bindEvent();
	}
};

$(document).ready(function() {
	sysRole.init();
});

//提供给外部刷新调用
function refashData(){
	sysRole.setPageChange(null);
	sysRole.search();
}


